package com.smyx.thread;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * 创建线程的三种方式
 * 1.继承Thread类重写run方法
 * 2.实现Runnable接口重写run方法
 * 3.实现Callable接口或者对象传递给Future再传递给Thread类（可以获取返回值）
 */
public class ThreadDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        // way1
        new ThreadWay01().start();
        // way2
        new Thread(new ThreadWay02()).start();
        // way3
        ThreadWay03 threadWay03 = new ThreadWay03();
        FutureTask<Integer> futureTask = new FutureTask<>(threadWay03);
        new Thread(futureTask).start();
        Integer integer = futureTask.get();
        System.out.println(integer);
    }
}

class ThreadWay01 extends Thread {
    @Override
    public void run() {
        System.out.println("Thread name = " + getName());
    }
}
class ThreadWay02 implements Runnable {
    @Override
    public void run() {
        System.out.println("Thread name = " + Thread.currentThread().getName());
    }
}
class ThreadWay03 implements Callable<Integer> {
    @Override
    public Integer call() {
        int sum = 0;
        for (int i = 0; i <= 100; i++) {
            sum = sum + i;
        }
        return sum;
    }
}
